﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YJKRevitArea.AreaShowHide
{
	public partial class AreaShowHideForm : System.Windows.Forms.Form
	{
		public AreaShowHideForm(bool bIfDrawn)
		{
			this.InitializeComponent();
			this.btnHide.Enabled = bIfDrawn;
		}

		private void btnShow_Click(object sender, EventArgs e)
		{
			List<AreaCategory> list = new List<AreaCategory>();
			if (this.chkBuild.Checked)
			{
				list.Add(AreaCategory.eAreaBuild);
			}
			if (this.chkFire.Checked)
			{
				list.Add(AreaCategory.eAreaFire);
			}
			if (this.chkInner.Checked)
			{
				list.Add(AreaCategory.eAreaInner);
			}
			if (list.Count <= 0)
			{
				return;
			}
			List<AreaCategory> list2 = CmdAreaShowHide._areaShowHideMag.ShowArea(list);
			if (list2.Count == list.Count)
			{
				this.btnHide.Enabled = false;
			}
			else
			{
				this.btnHide.Enabled = true;
			}
			string text = string.Empty;
			using (List<AreaCategory>.Enumerator enumerator = list2.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					switch (enumerator.Current)
					{
					case AreaCategory.eAreaBuild:
						text += "建筑面积 ";
						break;
					case AreaCategory.eAreaInner:
						text += "套内面积 ";
						break;
					case AreaCategory.eAreaFire:
						text += "防火分区 ";
						break;
					}
				}
			}
			if (!string.IsNullOrEmpty(text))
			{
				text += "面积轮廓未生成，请先在相应面积平面中绘制";
				YJKMessageBox.Show(text);
			}
		}

		private void btnHide_Click(object sender, EventArgs e)
		{
			List<AreaCategory> list = new List<AreaCategory>();
			if (this.chkBuild.Checked)
			{
				list.Add(AreaCategory.eAreaBuild);
			}
			if (this.chkFire.Checked)
			{
				list.Add(AreaCategory.eAreaFire);
			}
			if (this.chkInner.Checked)
			{
				list.Add(AreaCategory.eAreaInner);
			}
			if (list.Count <= 0)
			{
				return;
			}
			if (CmdAreaShowHide._areaShowHideMag.HideArea(list))
			{
				this.btnHide.Enabled = false;
			}
		}

		private void AreaShowHideForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private void AreaShowHideForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				this.RefreshSearchData();
			}
		}

		private void AreaShowHideForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			CmdAreaShowHide._areaShowHideMag.ClearOrigionFilledRegionType();
			this.RefreshSearchData();
		}

		public void RefreshSearchData()
		{
			this.ChangeOperateState();
			this.SimulationPressESC();
		}

		private void ChangeOperateState()
		{
			Cursor.Position = new System.Drawing.Point(Cursor.Position.X, base.Bottom + 1);
			this.SendSetfocusMessage();
			this.SimulationPressESC();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			AreaShowHideForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			AreaShowHideForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void SendSetfocusMessage()
		{
			int wMsg = 7;
			AreaShowHideForm.SendMessage(CmdAreaShowHide._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private bool m_bFromClose;

		private enum ERequestId
		{
			eDrawOutLine = 1,
			eDeleteOutLine,
			eNoOper
		}
	}
}
